import Vue from 'vue'
import App from './App.vue'
import router from "./router";
import store from "./store";
import axios from "axios"
import VueAxios from "vue-axios"
import Storage from "./plugins/storage"
import "./assets/fonts/iconfont.css"
import "./assets/css/global.css"

// 添加进度条效果
import NProgress from 'nprogress'

// 简单配置
NProgress.inc(0.2)
NProgress.configure({ easing: 'ease', speed: 500, showSpinner: false, trickle: false })

// 配置请求根路径
axios.defaults.baseURL = "http://127.0.0.1:8000/api/v1"
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'


axios.interceptors.request.use(config => {
  NProgress.start()
  config.headers.Authorization = Storage.sessionGet("token");
  return config
})

axios.interceptors.response.use( response => {
  NProgress.done()
  if (response.status >= 250  && response.status <= 260) {
      Storage.sessionRemove('token') // 删除已经失效或过期的token（不删除也可以，因为登录后覆盖）
      router.replace({path: '/Login'}).catch(err => {err})
  } else if (response.data.token) {
      Storage.sessionSet('token', response.data.token)
  }
  return response
})

Vue.filter("datetimeFormat", function(time){
    let oldDate = new Date(time)
    let year   = oldDate.getFullYear();
    let month  = oldDate.getMonth()+1;
    let day    = oldDate.getDate();
    let hour   = oldDate.getHours(); 
    let minute = oldDate.getMinutes(); 
    let second = oldDate.getSeconds(); 
    return year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
})

Vue.filter("dateFormat", function(time){
    if (time == '0001-01-01T00:00:00Z') return ''
    let oldDate = new Date(time)
    let year   = oldDate.getFullYear();
    let month  = oldDate.getMonth()+1;
    let day    = oldDate.getDate();
    let hour   = oldDate.getHours();
    let minute = oldDate.getMinutes();
    let second = oldDate.getSeconds();
    return year+"-"+month+"-"+day
})

Vue.config.productionTip = false
Vue.prototype.domain = "http://127.0.0.1:8000"
Vue.prototype.storage = Storage
Vue.use(VueAxios, axios)

new Vue({
  router,
  store,
  render: h => h(App),
}).$mount('#app')
